﻿using System;

using System.Collections.Generic;
using System.Text;
using Fluid.Controls;
using System.Drawing;
using PasswordSafe.Xml;
using PasswordSafe.Classes;
using PasswordSafe.Properties;
using System.Windows.Forms;

namespace PasswordSafe
{
    public class Pages : NavigationPanel
    {
        protected override void InitControl()
        {
            base.InitControl();
            BackColor = Color.Empty;

            categoriesPage = new PageControl("Categories");
            passwordsPage = new PageControl("Category");
            detailPage = new PageControl("Details");

            categoriesPage.Control = new CategoriesListBox();
            passwordsPage.Control = new PasswordsListBox();
            detailPage.Control = new DetailListBox();

            Image plusImage = Resources.plus;
            Image minusImage = Resources.minus;

            FluidButton newCategoryButton = new FluidButton(plusImage);
            newCategoryButton.Click += new EventHandler(newCategoryButton_Click);
            categoriesPage.Buttons.Add(newCategoryButton);

            FluidButton newPasswordButton = new FluidButton(plusImage);
            newPasswordButton.Click += new EventHandler(newPasswordButton_Click);
            passwordsPage.Buttons.Add(newPasswordButton);

            FluidButton delPasswordButton = new FluidButton(minusImage);
            delPasswordButton.Click += new EventHandler(delPasswordButton_Click);
            detailPage.Buttons.Add(delPasswordButton);

            FluidButton editCategoryBtn = new FluidButton();
            this.editCategoryBtn = editCategoryBtn;
            editCategoryBtn.Width = 40;
            editCategoryBtn.BackColor = Color.Empty;
            editCategoryBtn.Text = "Edit";
            categoriesPage.BackButton = editCategoryBtn;
            editCategoryBtn.Click += new EventHandler(editCategoryBtn_Click);


            Pages.Add(categoriesPage);
            Pages.Add(passwordsPage);
            Pages.Add(detailPage);

            SelectedIndex = 0;

            categoriesListBox = categoriesPage.Control as CategoriesListBox;
            passwordsListBox = passwordsPage.Control as PasswordsListBox;
            detailListBox = detailPage.Control as DetailListBox;

            categoriesListBox.CategorySelected += new EventHandler(categoriesListBox_CategorySelected);
            passwordsListBox.PasswordSelected += new EventHandler(categoryListBox_PasswordSelected);

        }

        public NotifyList Categories { get { return categoriesListBox.Categories; } }

        public NotifyList Passwords { get { return passwordsListBox.Passwords; } }

        public FluidButton editCategoryBtn;

        void editCategoryBtn_Click(object sender, EventArgs e)
        {
            categoriesListBox.EditMode ^= true;
        }


        PasswordData passwordToRemove;

        void delPasswordButton_Click(object sender, EventArgs e)
        {
            passwordToRemove = passwordsListBox.SelectedPassword;
            if (passwordToRemove != null)
            {
                MessageDialog dialog = new MessageDialog();
                dialog.Message = "Delete Password?";
                dialog.Result += new EventHandler<Fluid.Classes.DialogEventArgs>(DeleteDialogResult);
                dialog.ShowModal(ShowTransition.FromBottom);


            }
        }

        void DeleteDialogResult(object sender, Fluid.Classes.DialogEventArgs e)
        {
            switch (e.Result)
            {
                case DialogResult.OK:
                    ListBuilder.Instance.DeletePassword(passwordToRemove);
                    UpdatePasswords();
                    this.GoBack();
                    break;
            }
        }


        void newPasswordButton_Click(object sender, EventArgs e)
        {
            NavigateForward();
        }

        public void NavigateForward()
        {
            PasswordCategory category = categoriesListBox.SelectedCategory;
            PasswordData password = ListBuilder.Instance.CreateNewPassword(category);
            selectedPassword = password;
            if (password != null)
            {
                detailPage.Title = "Edit New Password";
                detailListBox.Details = ListBuilder.Instance.GetDetails(password, true);
                Forward();
            }
        }

        void newCategoryButton_Click(object sender, EventArgs e)
        {
            CategoriesPanel cp = CategoriesPanel.Instance;
            cp.UpdateData();
            cp.ShowMaximized(ShowTransition.FromBottom);
        }

        CategoriesListBox categoriesListBox;
        PasswordsListBox passwordsListBox;
        DetailListBox detailListBox;

        void categoryListBox_PasswordSelected(object sender, EventArgs e)
        {
            selectedPassword = passwordsListBox.SelectedPassword;
            UpdateDetails();
        }


        private void UpdateDetails()
        {
            PasswordsListBox lb = passwordsListBox;
            PasswordData password = lb.SelectedPassword;
            if (password != null)
            {
                detailPage.Title = password.Name;
                detailListBox.Details = ListBuilder.Instance.GetDetails(password, true);
                Forward();
            }
        }


        void categoriesListBox_CategorySelected(object sender, EventArgs e)
        {
            selectedCategory = categoriesListBox.SelectedCategory;
            if (UpdatePasswords()) Forward();
        }

        private NotifyList GetPasswordList()
        {
            CategoriesListBox lb = categoriesListBox;
            PasswordCategory category = lb.SelectedCategory;

            if (category != null)
            {
                passwordsPage.Title = category.Title;
                return ListBuilder.Instance.GetPasswords(category, false);
            }
            return null;
        }

        private SearchPanel searchPanel;

        public void ShowSearchPanel()
        {
            if (this.SelectedIndex == 1)
            {
                searchPanel = SearchPanel.Instance;
                searchPanel.Showing += new EventHandler(searchPanel_Showing);
                searchPanel.Closing += new EventHandler(searchPanel_Closing);
                searchPanel.Show(ShowTransition.FromBottom);

            }
        }

        void searchPanel_Showing(object sender, EventArgs e)
        {
            passwordsListBox.MaxHeightOffset = passwordsListBox.ScreenBounds.Bottom - (Host.Bounds.Height - searchPanel.ScreenBounds.Height);
            passwordsListBox.Focus();
        }

        void searchPanel_Closing(object sender, EventArgs e)
        {
            passwordsListBox.MaxHeightOffset = 0;
        }


        protected override void BeforeNavigate(int index, bool back)
        {
            if (searchPanel != null) searchPanel.Close(ShowTransition.None);
            base.BeforeNavigate(index, back);
            if (back)
            {
                switch (index)
                {
                    case 0: CheckCategoriesList(); break;
                    case 1: CheckPasswordsList(); break;
                    case 2: CheckDetailsList(); break;
                }
            }
        }


        private void CheckCategoriesList()
        {
            if (passwordsListBox.Passwords.Modified)
            {
                passwordsListBox.Passwords.Modified = false;
                UpdateCategories();
                if (selectedCategory != null) categoriesListBox.SelectedItemIndex = categoriesListBox.Categories.IndexOf(selectedCategory);
                categoriesListBox.EnsureVisible(categoriesListBox.SelectedItemIndex);
            }
        }

        private void UpdateCategories()
        {
        }

        private void CheckDetailsList()
        {

        }

        /// <summary>
        /// The PasswordData that was selected before navigating to the details.
        /// </summary>
        private PasswordData selectedPassword;

        /// <summary>
        /// The PasswordCategory that was selected before navigating to the details.
        /// </summary>
        private PasswordCategory selectedCategory;


        private bool UpdatePasswords()
        {
            CategoriesListBox lb = categoriesListBox;
            PasswordCategory category = lb.SelectedCategory;

            if (category != null)
            {
                passwordsPage.Title = category.Title;
                passwordsListBox.Passwords = ListBuilder.Instance.GetPasswords(category, true);
                passwordsPage.Buttons[0].Visible = !category.ReadOnly;
                return true;
            }
            return false;
        }

        private void CheckPasswordsList()
        {
            if (detailListBox.Details.Modified)
            {
                detailListBox.Details.Modified = false;
                UpdatePasswords();
                if (selectedPassword != null)
                {
                    passwordsListBox.SelectedItemIndex = passwordsListBox.Passwords.IndexOf(selectedPassword);
                    passwordsListBox.EnsureVisible(passwordsListBox.SelectedItemIndex);
                }
            }
        }

        private PageControl categoriesPage;
        private PageControl passwordsPage;
        private PageControl detailPage;

        public bool CategoryEdit
        {
            get { return categoriesListBox.EditMode; }
            set { categoriesListBox.EditMode = value; }
        }

    }
}
